$(function() {

	
	/*为 test 绑定事件*/
	$("a.tableAction.menu").on('click', function() {
		var tableId = $(this).attr('data-table-id');
		var status = $(this).parent().parent().parent().parent().find("span.rstblock-left-timeout").text();
		if (status == "空闲中" || status == "预定中") {
			window.location.href='/menu?table='+tableId;
		}else{
			alert("该餐桌已在用餐中...");
		}
	});
	
	/*为 test 绑定事件*/
	$("a.tableAction.addDetail").on('click', function() {
		var tableId = $(this).attr('data-table-id');
		var status = $(this).parent().parent().parent().parent().find("span.rstblock-left-timeout").text();
		if (status == "空闲中" || status == "预定中") {
			alert("该餐桌还无人用餐...");
		}else{
			window.location.href='/menu?table='+tableId;
		}
	});
	
	/*为 test 绑定事件*/
	$("a.tableAction.order").on('click', function() {
		var tableId = $(this).attr('data-table-id');
		var status = $(this).parent().parent().parent().parent().find("span.rstblock-left-timeout").text();
		if (status == "空闲中" || status == "预定中") {
			alert("该餐桌还无人用餐...");
		}else if (status == "用餐中"){
			$.ajax({
				type:'POST',
				url:'getOrderByTableId',
				dataType:'json',
				data:{
					tableId:tableId
				},
				success: function(data) {
					var message = '';
					message += "订单号："+data.id+"\n";
					message += "桌号："+data.dinnerTable.tableName+"\n";
					message += "下单时间："+data.time+"\n";
					message += "\n";
					message += "    菜名                    数量                    价格";
				 	message = message + "\n----------------------------------------------";
					var list = data.orderDetailList;
					for(var i = 0 ; i < list.length ; i++ ){
						message = message + "\n "+list[i].food.foodName;
						for (var j = 0; j < (10-list[i].food.foodName.length); j++) {
							message = message + "   ";
						}
						message += list[i].foodCount ;
						message += "份";
						for (var k = 0; k < 18; k++) {
							message = message + " ";
						}
						message += "￥";
						message += list[i].foodMoney;
					}
					message = message + "\n----------------------------------------------";
					message += "\n";
					message = message + "\n总金额: ￥"+data.totalPrice;
					message = message + "\n是否结账？";
					var flag = confirm(message);
					if (flag) {
						//alert("结账");
						$.ajax({
							type:'POST',
							url:'subOrder',
							dataType:'json',
							data:{
								tableId:tableId
							},
							success: function(data) {
								if (data.success) {
									alert("结账成功！");
									location.reload();
								}else{
									alert("结账失败！");
								}
							},
							error: function(data) {
								alert('error');
							}
						});
					}
				},
				error: function(data) {
					alert('error');
				}
			});
		}
	});
	
});

/*function setTable() {
	var tables = $("div.rstblock.dropdown");
	for (var i = 0; i < tables.length; i++) {
		var status = $(tables[i]).find("span.rstblock-left-timeout").text();
		if (status == "空闲中" || status == "预定中") {
			$(tables[i]).find("div.dropdown-content a.addDetail").addClass('disabled');
			$(tables[i]).find("div.dropdown-content a.order").addClass('disabled');
		}else if (status == "用餐中") {
			
		}else {}
	}
}*/

/* cookies */
//写cookies
function setCookie(name, value) {
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires="
			+ exp.toGMTString();
}

//读取cookies 
function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1
			c_end = document.cookie.indexOf(";", c_start)
			if (c_end == -1)
				c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end))
		}
	}
	return ""
}

//删除cookies 
function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if (cval != null)
		document.cookie = name + "=" + cval + ";expires="
				+ exp.toGMTString();
}

/* model 1 */
/*
<div style="position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; opacity: 0.5; background: rgb(0, 0, 0); z-index: 1002;"></div>
*/

/* model 2 */
/*
function getOrderHtml(orederId,time,list,totalPrice) {
	var h = '';
	h += '<div class="dialog" style="z-index: 1009; left: 390.5px; top: 109px;">';
		h += '<div class="dialog-close icon-close"></div>';
		h += '<div class="dialog-content" >';
			h += '<div class="ng-scope ng-isolate-scope">';
				h += '<div class="shop-iteminfo">';
					h += '<section class="iteminfo-info">';
						h += '<h5 class="ng-binding">麦乐鸡5块</h5>';
						h += '<p class="description ng-binding"  >时间：2017-10-10 12:21</p>';
						h += '<div class="iteminfo-rate">';
							h += '<h6 ng-if="ratingCount" class="ng-scope">';
								h += '商品列表<span ng-bind="ratingCount" class="ng-binding">（6）菜数量</span>';
							h += '</h6>';
							h += '<div>';
								h += '<ul class="rategroup">';
	
									h += '<li class="ng-scope">';
										h += '<div>';
											h += '<div title="评分5.0分" style="display: inline-block;">菜名</div>';
											h += '<span class="ng-binding"> x 6</span>';
											h += '<div class="rateuser">';
												h += '<span   class="ng-binding">价格</span>';
											h += '</div>';
										h += '</div>';
										h += '<p class="ratetext ng-binding" ></p>';
											
									h += '</li>';
								h += '</ul>';
							h += '</div>';
						h += '</div>';
						h += '<div class="iteminfo-cartitem">';
							h += '<div class="iteminfo-price">';
								h += '<span class="price"><span class="yen">¥</span> <span class="price ng-binding" >11</span></span>';
							h += '</div>';
							h += '<div  class="ng-scope ng-isolate-scope">';
								h += '<div  class="ng-scope">';
									h += '<button class="shop-cartbutton ng-binding ng-scope" >立即购买</button>';
								h += '</div>';
							h += '</div>';
						h += '</div>';
					h += '</section>';
				h += '</div>';
			h += '</div>';
		h += '</div>';
	h += '</div>';
}
/*

	
	
		
			
				
				
				
				
					
						
					
					
						
							
									
									
									
										
									
								
								
						
					
				
				<div class="iteminfo-cartitem">
					<div class="iteminfo-price">
						<span class="price"><span class="yen">¥</span> <span
							class="price ng-binding"
							ng-bind="(itemInfo.original_price || itemInfo.price) + (itemInfo.specfoods_id.length > 1 ? '起' : '')">11</span></span>
					</div>
					<div ng-if="itemInfo.name" shop-cartbutton="" food="itemInfo"
						button-text="立即购买" ng-hide="shop.id == '656683'"
						class="ng-scope ng-isolate-scope">
						<div ng-if="!menuFood.hasSpec" class="ng-scope">
							<button class="shop-cartbutton ng-binding ng-scope"
								ng-if="!cartItem.quantity &amp;&amp; menuFood.stock"
								ng-click="cartItem.add($event)">立即购买</button>
						</div>
					</div>
				</div>
				</section>
			</div>
		</div>
	</div>
</div>
 */